/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *  UICameraView.m - iPhoneSRV console for Surveyor SRV-1 and SVS 
 *
 *  Created by Nick Kitchener.  Copyright (C) 2009  Surveyor Corporation 
 *            and Nick Kitchener.  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details (www.gnu.org/licenses)
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#import "SRVUICameraView.h"

@implementation SRVUICameraView
@synthesize blackfinController, resolution, frameRate;

UIImage *foobarImage = nil;

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
		displayedImage =  nil;
		displayedOperation = nil;
		fetchOperation = nil;
		resolution = SRVImageResolution320x240;
		frameRate = 5.0;
    }
    return self;
}

-(void)dealloc {
	[super dealloc];
}

-(void)startImageFeed {
	
	time = 1.0/frameRate;
	
	timer = [NSTimer scheduledTimerWithTimeInterval:time
											 target:self
										   selector:@selector(timerFireMethod:)
										   userInfo:nil repeats:YES];
}

-(void)stopImageFeed {
	[timer invalidate];
	[timer release];
	timer = nil;
}

-(void)drawRect: (CGRect) rect {
	if(displayedImage!=nil) [displayedImage drawInRect:rect];
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

-(void)timerFireMethod:(NSTimer*)theTimer {
	if( fetchOperation == nil ) {
		fetchOperation = [[SRVGetJPGFrameOperation alloc] init];
		[fetchOperation setCallback:self];
		[fetchOperation setOperationTimeout:time];
		[blackfinController queueOperation:fetchOperation];
	}
}

-(void)operationCompleted:(SRVNSOperation *)operation {
		if([operation class] == [SRVSetImageResolutionOperation class]) {
			[operation release];
			operation = nil;
		} else {
			if( [operation expired] ) {
				[operation release];
				operation = nil;
			} else {
				@synchronized(self) {
				SRVGetJPGFrameOperation *releasedOperation = displayedOperation;
				UIImage					*releasedImage = displayedImage;
				
				displayedOperation = fetchOperation;
				fetchOperation = nil;
				displayedImage = [[UIImage imageWithData:[displayedOperation data]] retain];
				
				[self setNeedsDisplay];
				
				if( releasedImage != nil ) {
					[releasedImage release];
					releasedImage = nil;
				}
				if( releasedOperation != nil ) {
					[releasedOperation release];
					releasedOperation = nil;
				}
				}
			}
		}
}

// tells the camera view to select the resolution to match the provided box.
-(void)autoAdjustResolution:(CGRect)screen  {
	resolution = SRVImageResolution160x120;
	CGRect tempRect	= CGRectMake(0.0,0.0, screen.size.width, screen.size.height);
//	CGRect aRect		= CGRectMake(0.0,0.0, 160.0,120.0);
	
//	if( CGRectContainsRect(tempRect, aRect) ) {
//		resolution = SRVImageResolution160x120;
//		CGRect bRect = CGRectMake(0.0,0.0, 320.0,240.0);
//		if( CGRectContainsRect(tempRect, bRect) ) {
			resolution = SRVImageResolution320x240;			
			CGRect cRect = CGRectMake(0.0,0.0, 640.0,480.0);
			if( CGRectContainsRect(tempRect, cRect) ) {
				resolution = SRVImageResolution640x480;				
				CGRect dRect = CGRectMake(0.0,0.0, 1280.0,1024.0);
				if( CGRectContainsRect(tempRect, dRect) ) {
					resolution = SRVImageResolution1280x1024;
				}
			}
//		}
//	}
	NSLog(@"Setting new camera resolution: %c", resolution);
	
	// queue the resolution change
	SRVSetImageResolutionOperation *resolutionOp = [SRVSetImageResolutionOperation resolution:resolution];
	[resolutionOp setCallback:self];
	[blackfinController queueOperation:resolutionOp];
}

@end
